package com.gxa.modules.service.impl;

import com.gxa.common.vo.JsonResult;
import com.gxa.common.vo.R;
import com.gxa.modules.mapper.StartListMapper;
import com.gxa.modules.pojo.Start_List;
import com.gxa.modules.service.StartListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: zdk
 * Date: 2022/09/12 21:58
 * Description:
 * Version: V1.0
 */
@Service
public class StartListServiceImpl implements StartListService {

    @Autowired
    private StartListMapper startListMapper;
    @Override
    public JsonResult findStartListMessage(Integer currentPage, Integer pageSize) {
        JsonResult jsonResult = new JsonResult();
        try {
            //查询总条数
            int totalSize = startListMapper.countSales();
            jsonResult.setCount(totalSize);

            Integer param01 = (currentPage - 1) * pageSize;//分页第一个参数
            Integer param02 = pageSize; //分页第二个参数

            //查询后的数据
            List<Start_List> Message = startListMapper.findStartListMessage(param01, param02);
            System.out.println("------------1----------");
            //成功后返回的结果
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setData(Message);


        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员");
        }


        return jsonResult;
    }
//删除
    @Override
    public R delMessage(Integer depart_id) {
        R r = new R();
        try {
            Integer i = startListMapper.delMessage(depart_id);

            if (i > 0) {
                r.setCode(200);
                r.setMsg("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("删除失败");
        }
        return r;
    }
//修改为已完成
    @Override
    public R updateStartMessage(Integer depart_id) {
        R r = new R();
        try {
            Integer i = startListMapper.updateStartMessage(depart_id);

            if (i > 0) {
                r.setCode(200);
                r.setMsg("修改成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("修改失败");
        }
        return r;
    }
    /**
     * 查询装载状态的售后订单

     * @return
     */
    @Override
    public JsonResult findStartMessageByBeing(Integer currentPage, Integer pageSize,Integer status) {
        JsonResult jsonResult = new JsonResult();
        try {
            //查询条数
            int totalSize = startListMapper.countStateSales(status);
            jsonResult.setCount(totalSize);
            //查询后的数据
            Integer param01 = (currentPage - 1) * pageSize;//分页第一个参数
            Integer param02 = pageSize; //分页第二个参数
            List<Start_List> afterSalesList = startListMapper.findStartMessageByBeing(param01,param02,status);
            System.out.println("------------1----------");
            //成功后返回的结果
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setData(afterSalesList);

        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员");
        }
        return jsonResult;
    }

    /**
     *查询运载状态的售后订单

     * @return
     */
    @Override
    public JsonResult findStartMessageByUntreated(Integer currentPage, Integer pageSize,Integer status) {
        JsonResult jsonResult = new JsonResult();
        try {
            //查询条数
            int totalSize = startListMapper.countStateSales(status);
            jsonResult.setCount(totalSize);
            //查询后的数据
            Integer param01 = (currentPage - 1) * pageSize;//分页第一个参数
            Integer param02 = pageSize; //分页第二个参数
            List<Start_List> afterSalesList = startListMapper.findStartMessageByUntreated(param01,param02,status);
            //成功后返回的结果
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setData(afterSalesList);

        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员");
        }
        return jsonResult;
    }

    /**
     *查询已完成状态的售后订单

     * @return
     */
    @Override
    public JsonResult findStartMessageByProcessed(Integer currentPage, Integer pageSize,Integer status) {
        JsonResult jsonResult = new JsonResult();
        try {
            //查询条数
            int totalSize = startListMapper.countStateSales(status);
            jsonResult.setCount(totalSize);
            //查询后的数据
            Integer param01 = (currentPage - 1) * pageSize;//分页第一个参数
            Integer param02 = pageSize; //分页第二个参数
            List<Start_List> afterSalesList = startListMapper.findStartMessageByProcessed(param01,param02,status);
            //成功后返回的结果
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setData(afterSalesList);

        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public R addStart(Start_List start_list) {
        R r = new R();
        try {
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String dateTime = format.format(date);
            Integer result = startListMapper.addStart(start_list);
            System.out.println(result);

            if (result>0){
                r.setCode(200);
                r.setMsg("添加成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    @Override
    public JsonResult findStartByOrder( HashMap map) {
        JsonResult jsonResult = new JsonResult();


        try {
            int currentPage = Integer.parseInt(map.get("currentPage").toString());
            int pageSize = Integer.parseInt(map.get("pageSize").toString());
            currentPage = (currentPage - 1) * pageSize;
            map.put("currentPage",currentPage);
            map.put("pageSize",pageSize);
            //查询总条数
            Integer totalSize = startListMapper.queryCountHigh(map);

            //查询后的数据
            List<Start_List> list = startListMapper.findStartByOrder(map);
            System.out.println(list);
            //成功后返回的结果
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setData(list);
            jsonResult.setCount(totalSize);

        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员");
        }


        return jsonResult;
    }
    }


